package global.com.loansapplication.ui.actviity.user;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jakewharton.rxbinding.view.RxView;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import javax.inject.Inject;

import butterknife.Bind;
import global.com.loansapplication.R;
import global.com.loansapplication.base.BaseActivity;
import global.com.loansapplication.bean.CustomInfo;
import global.com.loansapplication.bean.SettingInfo;
import global.com.loansapplication.bean.VoiceListInfo;
import global.com.loansapplication.comm.Constants;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.event.NetErrorEvent;
import global.com.loansapplication.event.RemoveEvent;
import global.com.loansapplication.event.SettingInfoEvent;
import global.com.loansapplication.ui.actviity.user.biz.CustomBiz;
import global.com.loansapplication.ui.actviity.user.component.DaggerCustomComponent;
import global.com.loansapplication.ui.actviity.user.contract.CustomContract;
import global.com.loansapplication.ui.actviity.user.module.CustomModule;
import global.com.loansapplication.ui.actviity.user.presenter.CustomPresenter;
import global.com.loansapplication.ui.widget.BounceScrollView;
import global.com.loansapplication.uploadbean.GetSelectVoice;
import global.com.loansapplication.util.CommonViewUtil;
import global.com.loansapplication.util.PreferencesUtils;
import global.com.loansapplication.util.StringUtils;
import global.com.loansapplication.util.SysUtils;
import global.com.loansapplication.util.ToastUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.functions.Action1;

/**
 * Created by admin on 2017/12/2.
 */

public class CustomAct extends BaseActivity implements CustomContract.View {


    @Inject
    CustomPresenter presenter;
    @Inject
    CustomBiz biz;
    @Bind(R.id.custom_details_money)
    TextView money;
    @Bind(R.id.custom_details_state)
    TextView state;
    @Bind(R.id.custom_details_name)
    TextView name;
    @Bind(R.id.custom_details_phone)
    TextView phone;
    @Bind(R.id.custom_details_city)
    TextView city;
    @Bind(R.id.custom_details_income)
    TextView income;
    @Bind(R.id.custom_details_job)
    TextView job;
    @Bind(R.id.custom_details_mouth)
    TextView mouth;
    @Bind(R.id.custom_details_year)
    TextView year;
    @Bind(R.id.custom_details_pay_type)
    TextView payType;
    @Bind(R.id.custom_details_social)
    TextView social;
    @Bind(R.id.custom_details_gongjijin)
    TextView gongjijin;
    @Bind(R.id.custom_details_room)
    TextView room;
    @Bind(R.id.custom_details_card)
    TextView card;
    @Bind(R.id.custom_details_baodan)
    TextView baodan;
    @Bind(R.id.custom_details_car)
    TextView car;
    @Bind(R.id.custom_details_sorce)
    TextView source;
    @Bind(R.id.custom_details_time)
    TextView time;
    @Bind(R.id.custom_details_pay)
    TextView pay;
    @Bind(R.id.needsx)
    TextView needsx;
    @Bind(R.id.custom_details_layout)
    LinearLayout parent;
    @Bind(R.id.custom_voice_info_layout)
    LinearLayout custom_voice_info_layout;
    @Bind(R.id.bounce_scroll_view)
    BounceScrollView bounceScrollView;
    @Bind(R.id.custom_details_pay_original_info)
    TextView originalInfo;
    @Bind(R.id.custom_details_pay_original)
    TextView original;
    @Bind(R.id.custom_details_pay_layout)
    LinearLayout payLayout;
    @Bind(R.id.custom_details_is_buy)
    TextView isBuyView;
    private String studentCode;
    boolean isCollection = false;
    boolean isCanPay = false;
    CustomInfo mCustomInfo;
    String mUid = "";
    PopupWindow pop;
    PopupWindow popPay;
    String position = "";
    DecimalFormat df = new DecimalFormat("0.00");

    @Bind(R.id.custom_details_voice_layout)
    LinearLayout voiceLayout;

    /**
     * 是否是自定义线索
     */
    boolean mIsCustom = false;
    String mUserPhone = "";
    private MediaPlayer mediaPlayer;
    private List<ImageView> imageViewList;
    private List<Integer> voiceLength;
    private Timer timer;
    private MyTimerTask timerTask;
    private int playVoicePosition, playImageCondition;
    private List<VoiceListInfo> voiceListBeanList;
    SettingInfo settingInfo = new SettingInfo();

    @Override
    protected void setRoot() {
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.activity_custom_details);

    }

    @Override
    protected void setup() {
        setStateText();
        setToolBar("客户详情", "");
        DaggerCustomComponent.builder().customModule(new CustomModule(this)).build().inject(this);
        presenter.setBiz(biz);

    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        setInfo();
        mUserPhone = getIntent().getStringExtra("userPhone");
        mIsCustom = getIntent().getBooleanExtra("isCustom", false);
        if (mIsCustom) {
            isBuyView.setVisibility(View.GONE);
            state.setVisibility(View.GONE);
        }
        studentCode = getIntent().getStringExtra(Constants.KEYSTRING);
        position = StringUtils.isEmpty(getIntent().getStringExtra("position")) ? "" : getIntent().getStringExtra("position");
        presenter.getInfo(studentCode, StringUtils.isEmpty(MyApplication.userInfo.getUseable()) ? "0" : MyApplication.userInfo.getUseable());
        presenter.selectVoice();
        if (!mIsCustom) {
            RxView.clicks(needsx)
                    .compose(this.<Void>bindToLifecycle())
                    .throttleFirst(500, TimeUnit.MILLISECONDS)
                    .subscribe(new Action1<Void>() {
                        @Override
                        public void call(Void aVoid) {
                            if (isCollection) {
                                if (StringUtils.isEmpty(mUid)) {
                                    showMsg("删除收藏失败");
                                } else {
                                    presenter.delCollection(mUid);
                                }
                            } else {
                                if (mCustomInfo == null || StringUtils.isEmpty(mCustomInfo.getStudentCode())) {
                                    showMsg("收藏失败");
                                } else {
                                    presenter.addCollection(mCustomInfo.getStudentCode());
                                }

                            }

                        }
                    });
        }

        RxView.clicks(payLayout)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.getPayInfo(studentCode, MyApplication.userInfo.getUseable());
                    }
                });

    }

    private void setInfo() {
        String setInfosStr = PreferencesUtils.getStringPreferences("settingInfo",
                "settingInfo", null);
        if (!TextUtils.isEmpty(setInfosStr)) {
            SettingInfo settingInfo = new Gson().fromJson(setInfosStr,
                    new TypeToken<SettingInfo>() {
                    }.getType());
            MyApplication.settingInfo = settingInfo;
        }

        if (MyApplication.settingInfo == null) {
            settingInfo.setCityIds("");
            settingInfo.setCustomerType(0);
            settingInfo.setMonth(0);
            settingInfo.sethavecar(0);
            settingInfo.sethavehouse(0);
            settingInfo.sethaveCreditCard(0);
            settingInfo.sethaveShebao(0);
            settingInfo.sethaveGongjijin(0);
            settingInfo.sethaveBaodan(0);
            settingInfo.sethaveYibao(0);
        } else {
            settingInfo.setCityIds(StringUtils.isEmpty(MyApplication.settingInfo.getCityIds()) ? "" : MyApplication.settingInfo.getCityIds());
            settingInfo.setCustomerType((MyApplication.settingInfo.getCustomerType() != null && Pattern.matches("^[0-3]", MyApplication.settingInfo.getCustomerType().toString())) ? MyApplication.settingInfo.getCustomerType() : 0);
            settingInfo.setMonth((MyApplication.settingInfo.getMonth() != null && Pattern.matches("^[0-5]", MyApplication.settingInfo.getMonth().toString())) ? MyApplication.settingInfo.getMonth() : 0);
            settingInfo.sethavecar((MyApplication.settingInfo.gethavecar() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethavecar().toString())) ? MyApplication.settingInfo.gethavecar() : 0);
            settingInfo.sethavehouse((MyApplication.settingInfo.gethavehouse() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethavehouse().toString())) ? MyApplication.settingInfo.gethavehouse() : 0);
            settingInfo.sethaveCreditCard((MyApplication.settingInfo.gethaveCreditCard() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveCreditCard().toString())) ? MyApplication.settingInfo.gethaveCreditCard() : 0);
            settingInfo.sethaveShebao((MyApplication.settingInfo.gethaveShebao() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveShebao().toString())) ? MyApplication.settingInfo.gethaveShebao() : 0);
            settingInfo.sethaveGongjijin((MyApplication.settingInfo.gethaveGongjijin() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveGongjijin().toString())) ? MyApplication.settingInfo.gethaveGongjijin() : 0);
            settingInfo.sethaveBaodan((MyApplication.settingInfo.gethaveBaodan() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveBaodan().toString())) ? MyApplication.settingInfo.gethaveBaodan() : 0);
            settingInfo.sethaveYibao((MyApplication.settingInfo.gethaveYibao() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveYibao().toString())) ? MyApplication.settingInfo.gethaveYibao() : 0);
        }
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    /***
     * Stop location service
     */
    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        recoveryVoice();
        recoveryTimer();
        super.onDestroy();
        biz.onDestory();

    }

    @Override
    public void showSuccess() {
        presenter.getInfo(studentCode, StringUtils.isEmpty(MyApplication.userInfo.getUseable()) ? "0" : MyApplication.userInfo.getUseable());
        if (!StringUtils.isEmpty(position)) {
            EventBus.getDefault().post(new RemoveEvent(Integer.parseInt(position)));
        }

        PreferencesUtils.setStringPreferences("settingInfo", "settingInfo", new Gson().toJson(settingInfo));
        EventBus.getDefault().post(new SettingInfoEvent(settingInfo));
    }

    @Override
    public void checkPay(int useCount) {
        if (isCanPay) {
            if (useCount == 1) {
                initPayPop(parent);
                popPay.showAtLocation(parent, Gravity.CENTER, 0, 0);
                return;
            }
            if (!PreferencesUtils.getBoolean(this, "isShowPopPay")) {
                PreferencesUtils.putBoolean(this, "isShowPopPay", true);
                initPop(parent);
                pop.showAtLocation(parent, Gravity.CENTER, 0, 0);
            } else {
                initPayPop(parent);
                popPay.showAtLocation(parent, Gravity.CENTER, 0, 0);
            }
        } else {
            showMsg("暂时不可以购买该线索");
        }
    }

    @Override
    public void showSelectVoice(List<VoiceListInfo> mVoiceListInfoList) {
        voiceListBeanList = mVoiceListInfoList;
        voiceLength = new ArrayList<>();
        if (voiceLength.size() < voiceListBeanList.size())
            getVoiceSecond(voiceLength.size());
    }

    @Override
    public void showFalseSelectVoice() {
        custom_voice_info_layout.setVisibility(View.GONE);
    }

    @Override
    public RequestBody getSelectVoice() {
        GetSelectVoice getSelectVoice = new GetSelectVoice();
        getSelectVoice.setFlyaskCode(studentCode);
        Gson gson = new Gson();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(JSON, gson.toJson(getSelectVoice));
        return requestBody;
    }

    @Override
    public Context curContext() {
        return this;
    }


    public void initPop(@Nullable ViewGroup container) {
        View view = getLayoutInflater().inflate(R.layout.pop_info, container, false);
        pop = new PopupWindow(this);
        pop.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        pop.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setBackgroundDrawable(new ColorDrawable(0x000000));
        TextView submit = (TextView) view.findViewById(R.id.pop_custom_info);
        RxView.touches(submit)
                .compose(this.<MotionEvent>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<MotionEvent>() {
                    @Override
                    public void call(MotionEvent motionEvent) {
                        initPayPop(parent);
                        popPay.showAtLocation(parent, Gravity.CENTER, 0, 0);
                        if (pop != null) {
                            pop.dismiss();
                        }
                        SysUtils.backgroundAlpha(CustomAct.this, 1f);
                    }
                });
        pop.setContentView(view);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                SysUtils.backgroundAlpha(CustomAct.this, 1f);
            }
        });
    }

    public void initPayPop(@Nullable ViewGroup container) {

        popPay = new PopupWindow(this);
        popPay.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        popPay.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        popPay.setFocusable(true);
        popPay.setOutsideTouchable(true);
        popPay.setBackgroundDrawable(new ColorDrawable(0x000000));
        View view = getLayoutInflater().inflate(R.layout.pop_pay, container, false);
        View layout = (View) view.findViewById(R.id.pop_pay_view);
        TextView money = (TextView) view.findViewById(R.id.pop_pay_money);
        TextView pay = (TextView) view.findViewById(R.id.pop_pay_submit);

        money.setText("¥  " + df.format(Double.valueOf(mCustomInfo.getPayAmount())));
        RxView.clicks(pay)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.getOrderId(mCustomInfo.getStudentCode(), df.format(Double.valueOf(mCustomInfo.getPayAmount())));
                        if (popPay != null) {
                            popPay.dismiss();
                        }
                        SysUtils.backgroundAlpha(CustomAct.this, 1f);
                    }
                });
        RxView.clicks(layout)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (popPay != null) {
                            popPay.dismiss();
                        }
                        SysUtils.backgroundAlpha(CustomAct.this, 1f);
                    }
                });

        popPay.setContentView(view);
        popPay.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                SysUtils.backgroundAlpha(CustomAct.this, 1f);
            }
        });
    }


    @Override
    public void upDateUI(CustomInfo customInfo) {
        mCustomInfo = customInfo;
        money.setText("" + customInfo.getPrice());
        if (StringUtils.isEmpty(customInfo.getRemark1())) {
            originalInfo.setVisibility(View.GONE);
            original.setVisibility(View.GONE);
        } else {
            originalInfo.setVisibility(View.GONE);
            original.setVisibility(View.GONE);
//            originalInfo.setVisibility(View.VISIBLE);
//            original.setVisibility(View.VISIBLE);
            original.setText("¥ " + customInfo.getRemark1());
        }
        String[] all = customInfo.getBuyersCode().trim().split(",");
        if (customInfo.getSoldQuantity() == customInfo.getUseCount()) {
            boolean isBuy = false;
            for (int i = 0; i < all.length; i++) {
                if (MyApplication.userInfo.getUserCode().equals(all[i]) && all[i] != null && !all[i].equals("")) {
                    isBuy = true;
                    break;
                }
            }
            if (isBuy) {
                isBuyView.setVisibility(View.GONE);
                state.setText("跟进中");
                state.setTextColor(Color.WHITE);
                state.setBackgroundResource(R.drawable.bg_shape_orange);
                payLayout.setVisibility(View.GONE);
            } else {
                state.setText("已抢完");
                if (!mIsCustom) {
                    isBuyView.setVisibility(View.VISIBLE);
                }
                state.setTextColor(Color.WHITE);
                state.setBackgroundResource(R.drawable.bg_shape_gray);
                pay.setText("已被" + customInfo.getUseCount() + "位抢完，无法购买");
                originalInfo.setVisibility(View.GONE);
                original.setVisibility(View.GONE);
                pay.setTextColor(Color.BLACK);
                payLayout.setBackgroundResource(R.drawable.bg_shape_gray);
                if (!mIsCustom) {
                    payLayout.setVisibility(View.VISIBLE);
                }
            }
            isCanPay = false;
        } else if (customInfo.getSoldQuantity() == 0) {
//            state.setText("马上抢");
//            state.setBackgroundResource(R.drawable.bg_shape_red);
            state.setText("已抢" + customInfo.getSoldQuantity() + "/" + customInfo.getUseCount());
            state.setBackgroundResource(R.drawable.bg_shape_green);
            if (!mIsCustom) {
                isBuyView.setVisibility(View.VISIBLE);
            }

            if (customInfo.getRemark1().equals(customInfo.getPayAmount())) {
                pay.setText("马上抢 现支付¥" + df.format(Double.valueOf(customInfo.getPayAmount())));
                originalInfo.setVisibility(View.GONE);
                original.setVisibility(View.GONE);
            } else {
                originalInfo.setVisibility(View.GONE);
                original.setVisibility(View.GONE);
//                originalInfo.setVisibility(View.VISIBLE);
//                original.setVisibility(View.VISIBLE);
                original.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                original.setText("¥ " + customInfo.getRemark1());
                pay.setText(" 现支付¥" + df.format(Double.valueOf(customInfo.getPayAmount())));
            }
            payLayout.setBackgroundResource(R.drawable.bg_shape_red);
            if (!mIsCustom) {
                payLayout.setVisibility(View.VISIBLE);
            }
            isCanPay = true;
        } else if (customInfo.getSoldQuantity() < customInfo.getUseCount() && customInfo.getSoldQuantity() > 0) {
            boolean isBuy = false;
            for (int i = 0; i < all.length; i++) {
                if (MyApplication.userInfo.getUserCode().equals(all[i]) && all[i] != null && !all[i].equals("")) {
                    isBuy = true;
                    break;
                }
            }
            if (isBuy) {
                isBuyView.setVisibility(View.GONE);
                state.setText("跟进中");
                state.setBackgroundResource(R.drawable.bg_shape_orange);
                payLayout.setBackgroundResource(R.drawable.bg_shape_gray);
                pay.setText("跟进中");
                payLayout.setVisibility(View.GONE);
                pay.setTextColor(Color.BLACK);
                isCanPay = false;
            } else {
                if (!mIsCustom) {
                    isBuyView.setVisibility(View.VISIBLE);
                }
                state.setText("已抢" + customInfo.getSoldQuantity() + "/" + customInfo.getUseCount());
                state.setBackgroundResource(R.drawable.bg_shape_green);
                if (customInfo.getRemark1().equals(customInfo.getPayAmount())) {
                    pay.setText("马上抢 现支付¥" + df.format(Double.valueOf(customInfo.getPayAmount())) + "(已有" + customInfo.getSoldQuantity() + "人抢单)");
                    originalInfo.setVisibility(View.GONE);
                    original.setVisibility(View.GONE);
                } else {
                    originalInfo.setVisibility(View.GONE);
                    original.setVisibility(View.GONE);
//                    originalInfo.setVisibility(View.VISIBLE);
//                    original.setVisibility(View.VISIBLE);
                    original.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    original.setText("¥ " + customInfo.getRemark1());
                    pay.setText("现支付¥" + df.format(Double.valueOf(customInfo.getPayAmount())) + "(已有" + customInfo.getSoldQuantity() + "人抢单)");
                }
                payLayout.setBackgroundResource(R.drawable.bg_shape_red);
                if (!mIsCustom) {
                    payLayout.setVisibility(View.VISIBLE);
                }
                isCanPay = true;
            }
        }
        year.setText(StringUtils.isEmpty(customInfo.getAge()) ? "" : customInfo.getAge());
        city.setText(StringUtils.isEmpty(customInfo.getCity()) ? "" : customInfo.getCity());
        income.setText(customInfo.getShouru() == null ? "" : customInfo.getShouru() + "");
        mouth.setText(customInfo.getMonth() + "个月");
        job.setText(StringUtils.isEmpty(customInfo.getZhiye()) ? "" : customInfo.getZhiye());
        name.setText(StringUtils.isEmpty(customInfo.getName()) ? "" : customInfo.getName());
        if (mIsCustom) {
            phone.setText(StringUtils.isEmpty(mUserPhone) ? "" : mUserPhone);
        } else {
            phone.setText(StringUtils.isEmpty(customInfo.getMobile()) ? "" : customInfo.getMobile());
        }
        if (StringUtils.isEmpty(customInfo.gethavecar()) || customInfo.gethavecar().equals("0")) {
            car.setText("不限");
        } else if (customInfo.gethavecar().equals("1")) {
            car.setText("有");
        } else if (customInfo.gethavecar().equals("2")) {
            car.setText("没有");
        }
        if (StringUtils.isEmpty(customInfo.gethavehouse()) || customInfo.gethavehouse().equals("0")) {
            room.setText("不限");
        } else if (customInfo.gethavehouse().equals("1")) {
            room.setText("有");
        } else if (customInfo.gethavehouse().equals("2")) {
            room.setText("没有");
        }
        if (StringUtils.isEmpty(customInfo.gethaveCreditCard()) || customInfo.gethaveCreditCard().equals("0")) {
            card.setText("不限");
        } else if (customInfo.gethaveCreditCard().equals("1")) {
            card.setText("有");
        } else if (customInfo.gethaveCreditCard().equals("2")) {
            card.setText("没有");
        }
        if (StringUtils.isEmpty(customInfo.gethaveShebao()) || customInfo.gethaveShebao().equals("0")) {
            social.setText("不限");
        } else if (customInfo.gethaveShebao().equals("1")) {
            social.setText("有");
        } else if (customInfo.gethaveShebao().equals("2")) {
            social.setText("没有");
        }
        if (StringUtils.isEmpty(customInfo.gethaveGongjijin()) || customInfo.gethaveGongjijin().equals("0")) {
            gongjijin.setText("不限");
        } else if (customInfo.gethaveGongjijin().equals("1")) {
            gongjijin.setText("有");
        } else if (customInfo.gethaveGongjijin().equals("2")) {
            gongjijin.setText("没有");
        }
        if (StringUtils.isEmpty(customInfo.gethaveBaodan()) || customInfo.gethaveBaodan().equals("0")) {
            baodan.setText("不限");
        } else if (customInfo.gethaveBaodan().equals("1")) {
            baodan.setText("有");
        } else if (customInfo.gethaveBaodan().equals("2")) {
            baodan.setText("没有");
        }
        payType.setText(StringUtils.isEmpty(customInfo.getTrainingType()) ? "" : customInfo.getTrainingType());
        if (!mIsCustom) {
            if (StringUtils.isEmpty(customInfo.getUid())) {
                CommonViewUtil.setDarw(needsx, this, R.mipmap.icon_collection_false, -1,
                        -1, -1);
                isCollection = false;
            } else {
                CommonViewUtil.setDarw(needsx, this, R.mipmap.icon_collection_true, -1,
                        -1, -1);
                isCollection = true;
                mUid = customInfo.getUid();
            }
        }
        source.setText(StringUtils.isEmpty(customInfo.getSource()) ? "" : customInfo.getSource());
        time.setText(StringUtils.isEmpty(customInfo.getUpdateTime()) ? "" : SysUtils.getDatePoorNew(new Date(), customInfo.getUpdateTime()) + "前");
        bounceScrollView.setVisibility(View.VISIBLE);

    }

    @Override
    public void setCol(boolean isCol, String uid) {
        if (mIsCustom) {
            return;
        }
        if (isCol) {
            CommonViewUtil.setDarw(needsx, this, R.mipmap.icon_collection_true, -1,
                    -1, -1);
            isCollection = true;
            mUid = uid;
        } else {
            CommonViewUtil.setDarw(needsx, this, R.mipmap.icon_collection_false, -1,
                    -1, -1);
            isCollection = false;
            mUid = uid;
        }
    }


    private void playVoice(final int position, String url) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        } else {
            recoveryVoice();
            mediaPlayer = new MediaPlayer();
        }
        try {
            //播放地址
            mediaPlayer.setDataSource(url);
            // 设置音频流的类型
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            // 通过异步的方式装载媒体资源
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    playVoicePosition = position;
                    recoveryVoiceImg();

                    recoveryTimer();
                    timer = new Timer();
                    timerTask = new MyTimerTask();
                    timer.schedule(timerTask, 0, 200);
                    // 装载完毕 开始播放流媒体
                    mediaPlayer.start();
                }
            });
            // 设置循环播放
            // mediaPlayer.setLooping(true);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // 在播放完毕被回调
                    recoveryTimer();
                    recoveryVoiceImg();
                }
            });

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    // 如果发生错误，重新播放
                    ToastUtils.shortToast("播放失败");
                    return false;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
            ToastUtils.shortToast("播放失败");
        }
    }

    private void recoveryVoice() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    private void recoveryVoiceImg() {
        playImageCondition = 0;
        for (ImageView imageView : imageViewList) {
            imageView.setImageResource(R.mipmap.voice_img3);
        }
    }

    private void recoveryTimer() {
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
        if (timer != null) {
            timer.cancel();
            timer.purge();
            timer = null;
        }
    }

    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    playImageCondition++;
                    switch (playImageCondition % 3) {
                        case 0:
                            imageViewList.get(playVoicePosition).setImageResource(R.mipmap.voice_img3);
                            break;
                        case 1:
                            imageViewList.get(playVoicePosition).setImageResource(R.mipmap.voice_img1);
                            break;
                        case 2:
                            imageViewList.get(playVoicePosition).setImageResource(R.mipmap.voice_img2);
                            break;
                        default:
                            break;
                    }
                }
            });
        }
    }


    private void getVoiceSecond(int count) {
        try {
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayer();
            } else {
                recoveryVoice();
                mediaPlayer = new MediaPlayer();
            }
            mediaPlayer.setDataSource(voiceListBeanList.get(count).getPath());
            // 设置音频流的类型
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            // 通过异步的方式装载媒体资源
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    voiceLength.add(mediaPlayer.getDuration() / 1000);
                    if (voiceLength.size() < voiceListBeanList.size())
                        getVoiceSecond(voiceLength.size());
                    else
                        showVoiceView();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showVoiceView() {
        imageViewList = new ArrayList<>();
        for (int i = 0; i < voiceListBeanList.size(); i++) {
            View voiceView = LayoutInflater.from(this).inflate(R.layout.item_voice, null);
            LinearLayout item_voice_layout = (LinearLayout) voiceView.findViewById(R.id.item_voice_layout);
            ImageView item_voice_img = (ImageView) voiceView.findViewById(R.id.item_voice_img);
            TextView item_voice_time = (TextView) voiceView.findViewById(R.id.item_voice_time);
            imageViewList.add(item_voice_img);
            item_voice_time.setText(voiceListBeanList.get(i).getTime() + "''");

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) item_voice_layout.getLayoutParams();
            layoutParams.width = 200 + 300 / 60 * voiceLength.get(i);
            item_voice_layout.setLayoutParams(layoutParams);

            final int finalI = i;
            item_voice_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    playVoice(finalI, voiceListBeanList.get(finalI).getPath());
                }
            });
            voiceLayout.addView(voiceView);
        }
    }
}
